extends Node2D

var unit_size = Vector2i(60, 60)
var map_size = Vector2i(8, 10)
var offset = Vector2i.ZERO
## 回合 / 红方黑方
enum TURN {
	RED, ## 红方
	BLACK ## 黑方
} 
# 回合 / 红方黑方名称
const TURN_NAMES = {
	TURN.RED: "RED",
	TURN.BLACK: "BLACK"
}
## 棋子类型
enum TYPE {
	GENERAL, ## 将/帅
	ADVISOR, ## 士/仕
	ELEPHANT, ## 象/相
	HORSE, ## 马
	CHARIOT, ## 车
	CANNON, ## 炮
	SOLDIER ## 兵/卒
}
## 棋子类型名称
const TYPE_NAMES = {
	TYPE.GENERAL: "GENERAL",
	TYPE.ADVISOR: "ADVISOR",
	TYPE.ELEPHANT: "ELEPHANT",
	TYPE.HORSE: "HORSE",
	TYPE.CHARIOT: "CHARIOT",
	TYPE.CANNON: "CANNON",
	TYPE.SOLDIER: "SOLDIER"
}

## 棋子价值
const PIECE_VALUES = {
	TYPE.GENERAL: 9999,
	TYPE.ADVISOR: 120,
	TYPE.ELEPHANT: 120,
	TYPE.HORSE: 250,
	TYPE.CHARIOT: 500,
	TYPE.CANNON: 250,
	TYPE.SOLDIER: 50 #过河之后价值加20
}

func _ready() -> void:
	center_offset()

# 居中偏移
func center_offset() -> void:
	var screen_size = get_viewport().get_visible_rect().size
	offset = Vector2i(
		(screen_size.x - (map_size.x * unit_size.x)) / 2,
		(screen_size.y - (map_size.y * unit_size.y)) / 2
	)

## 全局作边换网格坐标
func pos_to_grid_pos(pos: Vector2) -> Vector2i:
	pos.x = pos.x - offset.x
	pos.y = pos.y - offset.y
	var x = int(pos.x) / unit_size.x
	var y = int(pos.y) / unit_size.y
	if y > 4: y -= 1
	return Vector2i(x, y)

## 全局棋盘网格坐标
func board_grid_pos() -> Array[Vector2i]:
	var positions: Array[Vector2i] = []
	for y in map_size.y:
		for x in map_size.x:
			if y >= 5:
				positions.append(Vector2i(x, y - 1))
			else:
				positions.append(Vector2i(x, y))
	return positions

## 全局网格坐标转位置
func grid_pos_to_pos(grid_pos: Vector2i) -> Vector2:
	var x = grid_pos.x * unit_size.x + offset.x
	var y = grid_pos.y * unit_size.y + offset.y
	if grid_pos.y >= 5:
		y += unit_size.y
	return Vector2(x, y)